<?php
/**
 * This file contains the class MemberModel which provides
 * most of the code requirements for the Member page
 *
 * @copyright  2009 A Memory Tree
 * @license    A Memory Tree License
 * @version    $Id:$
 * @link       http://my-trac.assembla.com/amemorytree/ticket/??
 * @since      File available since Release 0.1
 */
/**
 * This class provides the code for the Member page
 *
 * @copyright  2009 A Memory Tree
 * @license    A Memory Tree License
 * @version    Release: @package_version@
 * @link       http://my-trac.assembla.com/amemorytree/ticket/??
 * @since      Class available since Release 0.1
 */
class MemberModel
{
    /**
     * Access to the database methods
     *
     * @var UsersDB
     */
    private $_db;
    /**
     * Access to the View methods
     *
     * @var MemberView
     */
    private $_view;
    /**
     * Create a new instance of this class
     */
    public function __construct($db, $view)
    {
        $this->_db = $db;
        $this->_view = $view;
    }
    /**
     * Check if the authorisation is correct, and if it is, save the password to the user account
     *
     * @return bool
     * @param int $userId
     * @param string $password
     * @param string $authCode
     */
    function processAuthorisedPassword($userId, $password, $authCode)
    {
        $db = $this->_db;
        $isValid = FALSE;
        if ($authCode === $db->getAuthCode($userId)) {
            $db->setPassword($userId, crypt($password, 'm3m0ry7r33'));
            $db->removeAuthCode($userId);
            $isValid = TRUE;
        }
        return $isValid;
    }
    /**
     * Check if the password was used to create the passCode
     *
     * @return bool
     * @param string $password
     * @param string $passCode
     */
    function isValidPassword($password, $passCode)
    {
        $isValid = crypt($password, $passCode) == $passCode;
        return $isValid;
    }
    /**
     * Check if the password is valid for the given user id
     *
     * @return bool
     * @param int $userId
     * @param string $password
     */
    function checkUserPassword($userId, $password)
    {
        $db = $this->_db;
        $passCode = $db->getPassword($userId);
        $isValid = $this->isValidPassword($password, $passCode);
        return $isValid;
    }
    /**
     * Reset the session variables relating to the user login back to their default values
     *
     * @return bool
     * @param mixed &$_SESSION
     */
    function resetSession(&$session)
    {
        $session['user']="";
        $session['failLogin']=0;
    }
    /**
     * Change the password of a users account
     *
     * This change occurs only if the old password is correctly given, and
     * the new password correctly matches the duplicate confirmation password
     *
     * @return string
     * @param int $userId
     * @param array[string]mixed $passwords
     */
    function processNewPassword($userId, $passwords)
    {
        $db = $this->_db;
        $old = $db->escape($passwords['old']);
        $encrypted = $db->escape($passwords['encrypted']);
        $new = $db->escape($passwords['new']);
        $confirm = $db->escape($passwords['confirm']);
        $result = 'incorrect';
        if ($this->isValidPassword($old, $encrypted)) {
            if ($new === $confirm) {
                $db->setPassword($userId, $new);
                $result = 'changed';
            } else {
                $result = 'unchanged';
            }
        }
        return $result;
    }
    /**
     * Convert the permissions, provided as a CSV list, into a series of links
     *
     * @return string
     * @param string $permissions
     */
    function permissionLinks($permissions)
    {
        $view = $this->_view;
        $links = '';
        $permissionList = explode(',', $permissions);
        foreach ($permissionList as $permission) {
            switch ($permission) {
                case "Admin Area":
                    $links .= $view->text('adminControlLink');
                    break;
                case "FD Submit":
                    $links .= $view->text('addListingLink');
                    break;
                default:
            }
        }
        return $links;
    }
}
?>